<template>
	<view class="flex-colume-start content meterInfoPage">
		<view class="flex-colume-start whiteBox" v-if="detailInfo">
			<view class="flex-row-start item1">
				<view class="title1">旧电表编号：</view>
				<view class="desc1">{{detailInfo.number}}</view>
			</view>
			<view class="flex-row-start item1">
				<view class="title1">电表名称：</view>
				<view class="desc1">{{detailInfo.name}} / {{detailInfo.jiexian}} / {{detailInfo.beishu}}</view>
			</view>
			<view class="flex-row-start item1">
				<view class="title1">地  址：</view>
				<view class="desc1">{{detailInfo.address}}</view>
			</view>
			<view class="flex-row-start item1">
				<view class="title1">最后读数：</view>
				<view class="desc1" style="color: rgb(255, 68, 68);">{{detailInfo.last_degrees}}</view>
			</view>
			<view style="margin-top: 40rpx; font-size: 30rpx; font-weight: 500;">更换电表（点击下面扫描按钮）</view>
			<view @click="scanBtnClick()" class="scanBtn">扫描新电表码</view>
			
			<view class="flex-row-start item2">
				<view class="title2" style="color: rgb(255, 68, 68);">新电表：</view>
				<u--input type="digit" class="input1" border="surround" 
					v-model="loginForm.new_number">
				
				</u--input>
			</view>
			<view class="flex-row-start item2">
				<view class="title2">旧电表读数：</view>
				<u--input class="input1" type="digit" border="surround" 
					v-model="loginForm.last_degrees">
			
				</u--input>
			</view>
			<view class="flex-row-start item2">
				<view class="title2">照片：</view>
				<u-upload :fileList="fileList1" @afterRead="afterReadPic" @delete="deletePic" name="1"
					:maxCount="1"></u-upload>
			</view>
			
			<u-button style="margin-top: 60rpx; height: 88rpx; border-radius: 44rpx;" class="sendBtn" loadingSize="10"
				@click="submitBtnClick()" :loading="sureLoading" :loadingText="loadingText">提交</u-button>
		</view>
		
		
	</view>
</template>

<script>
	import urlConfig from '@/api/config.js'
	import { meterInfo, taskSubmit } from '@/api/api' 
	export default {
		data() {
			return {
				sureLoading:false,
				loadingText:'图片上传中',
				loginForm:{
					number:'',//老电表
					new_number:'',//新电表号码,
					old_degrees:undefined,
					last_degrees:undefined
					
				},
				fileList1:[],
				detailInfo:undefined
				
			}
		},
		onLoad(e) {
			meterInfo({number:e.code}).then(res=>{
				if(res.code == 1){
					this.detailInfo = res.data
					this.loginForm.number = this.detailInfo.number
					this.loginForm.last_degrees = this.detailInfo.last_degrees
					this.loginForm.old_degrees = this.detailInfo.last_degrees
					return
					
				}
				uni.showModal({
					content:res.msg,
					showCancel:false,
					success:res1=>{
						if (res1.confirm){
							uni.navigateBack()
						}
					}
				})
				
				
			})
			
		},
		methods: {
			submitBtnClick(){
				if (!this.loginForm.new_number){
					uni.showToast({
						title:'请填写新电表号码',
						icon:'none'
					})
					return
				}
				if(this.fileList1.length > 0){
					this.loginForm.img = this.fileList1[0].url
				}
				this.sureLoading = true
				this.loadingText = '提交'
				taskSubmit({...this.loginForm}).then(res=>{
					this.sureLoading = false
					if (res.code == 1){
						uni.showModal({
							content:'成功!',
							showCancel:false,
							success:res2=>{
								uni.navigateBack()
							}
						})
						return
					}
					uni.showToast({
						title:res.msg,
						icon:'none'
					})
				}).catch(err=>{
					this.sureLoading = false
				})
			},
			scanBtnClick(){
				uni.scanCode({
					success: (res) => {
						var result = res.result
						this.loginForm.new_number = result
					}
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterReadPic(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url, event.name)
					var result1 = JSON.parse(result)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: result1.code == 1? 'success':'fail',
						message: '',
						url: result1.data.imgPath
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url, eventName) {
				return new Promise((resolve, reject) => {
					this.sureLoading = true
					this.loadingText = '图片上传中'
					var url1 =  urlConfig + '/api/uploadImg' 
					var body = {
						'uploadImg':url,
						'utoken': uni.getStorageSync('ele-mix-token')
					}
					let a = uni.uploadFile({
						url: url1, // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'uploadImg',
						formData: {
							'utoken': uni.getStorageSync('ele-mix-token')
						},
						header: {
							'utoken': uni.getStorageSync('ele-mix-token'),
							"content-type": "application/x-www-form-urlencoded"
						},
						success: (res) => {
							setTimeout(() => {
								this.sureLoading = false
								resolve(res.data)
							}, 1000)
						}
					});
				})
			}
		}
	}
</script>

<style>
	page{
		background-color: #f8f8f8;
	}
</style>

<style lang="scss" scoped>

	
	.meterInfoPage{
		.sendBtn {
			color: white;
			background-color: #4985ff;
			height: 72px;
			border-color: white;
		}
		.whiteBox{
			align-items: flex-start;
			margin-top: 25rpx;
			width: 710rpx; 
			border-radius: 16rpx;
			padding: 25rpx;
			box-sizing: border-box;
			background-color: white;
			.item1{
				align-items: flex-start;
				font-size: 26rpx;
				margin: 12rpx 0rpx;
				color: rgb(150, 150, 150);
				.title1{
					font-size: 28rpx;
					width: 180rpx;
				}
				
			}
			.scanBtn{
				margin-top: 30rpx;
				margin-bottom: 35rpx;
				border: 1px solid #d3e2ff;
				padding: 20rpx 30rpx;
				box-sizing: border-box;
				border-radius: 10rpx;
				font-size: 28rpx; 
				color: #3277ff;
	
			}
			.item2{
				width: 100%;
				margin-bottom: 20rpx;
				
				.title2{
					font-size: 28rpx;
					width: 180rpx;
					margin-right: 5rpx;
					
				}
				.input1{
					padding-top: 0rpx;
					padding-bottom: 0rpx;
					box-sizing: border-box;
					height: 70rpx;
					font-size: 26rpx;
					font-weight: 500;
					flex: 1;
				}
				
			}
			
		}
	}
	
	

</style>
